<style type="text/css">
#pageCate { margin: 10px; width: 80%; }
#pageCate h3 { text-align: center; display: none; }
#pageCate table { margin: 10px; }
#pageCate table tr th { background-color: #000; color: #f7f7f7; text-align: left;  }
#pageCate td.name input { width: 150px; color: #888; }
#pageCate td.sortid input { width: 50px; color: #888 }
div.alert { margin: 10px 1px 10px 10px;}
td.c { text-align: center; }
td img { cursor: pointer; }
</style>

<div id="pageCate">
	<h3>类别配置</h3>
	<table class="table table-bordered table-condensed">
	<tr>
		<th>类别名称</th>
		<th>排序</th>
		<th>保存</th>
		<th>删除</th>
	</tr>
	<% @catelist.each do|cate| %>
	<tr vid="<%=cate.id%>">
		<td class="name"><input type="text" value='<%=cate.name%>' maxlength="8" /></td>
		<td class="sortid c"><input type="text" value='<%=cate.sortid%>' maxlength="1" /></td>
		<td class="c"><img src="/images/save.png" /></td>
		<td class="c"><%if(@catelist.size>1)%><img src="/images/trash.gif" /><%end%></td>
	</tr>
	<% end %>
	<% if(@catelist.size < 6) %>
	<tr id="trNewData">
		<td class="name"><input type="text" value='' maxlength="8" />
			&nbsp;&nbsp;<span class="label label-info">新增类别</span>
		</td>
		<td class="sortid"><input type="text" value='0' maxlength="1" /></td>
		<td class="c"><img src="/images/save.png" onclick="__new();" /></td>
		<td class="c"></td>
	</tr>
	<% end %>
	</table>
	<div class="alert alert-info">
		- 类别配置数量限制：1-6 个<br/>
		- 类别名称：不能超过 8 个字符<br/>
		- 当类别中有文档存在时不允许删除
	</div>
</div>

<script type="text/javascript">

// 获取类别ID号
function getCateID(img) {
	var _tr = $(img).parents('tr:first');
	var cateID = _tr.attr('vid');
	return cateID;
}

// 保存类别
function __save(img) {
	var cateID = getCateID(img);
	var _tr = $(img).parents('tr:first');
	var name = _tr.find('td:eq(0) input').val();
	var sortid = _tr.find('td:eq(1) input').val();
	$.post('/doc/cate_save', {
		'id':cateID,
		'name': name,
		'sortid':sortid
	}, function(data){
		alert('保存' + (data == 1 ? '成功' : '失败'));
		if(data == 1) location.href = location.href;
	}, 'json');
}

// 删除类别
function __delete(img) {
	if(confirm('确定要删除么？')) {
		var cateID = getCateID(img);
		$.post('/doc/cate_delete', {
			'id':cateID
		}, function(data){
			if(data == 1){
				alert('删除成功');
				location.href = location.href;
			} else if(data == -1) {
				alert('该类别仍存在文档，不允许删除。');
			} else {
				alert('删除失败');
			}
		}, 'json');
	}
}

// 新增类别
function __new() {
	var _tr = $('#trNewData');
	var name = _tr.find('td:eq(0) input').val();
	var sortid = _tr.find('td:eq(1) input').val();
	$.post('/doc/cate_new', {
		'name': name,
		'sortid':sortid
	}, function(data){
		alert('保存' + (data == 1 ? '成功' : '失败'));
		if(data == 1) location.href = location.href;	
	}, 'json');
}

$().ready(function(){

	// 绑定事件
	$('tr[vid] img').click(function(){
		if(this.src.toLowerCase().indexOf('save.') != -1){
			__save(this);	
		} else {
			__delete(this);
		}
	});	

});
</script>


